import { ipcMain } from "electron";
import * as path from "path";
import fs from "fs";
import * as os from "os";

export function rootDir() {
  if (process.env.NODE_ENV === "development") {
    return "./";
  } else {
    return os.homedir();
  }
}

export function cacheDir() {
  const dir = path.join(rootDir(), ".bbx");
  // 不存在目录，创建之
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir, { recursive: true });
  }
  return dir;
}

export const initDir = () => {
  ipcMain.handle("rootDir", () => {
    return rootDir();
  });
  ipcMain.handle("cacheDir", () => {
    return cacheDir();
  });
};
